package com.keke.applay.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.keke.applay.service.ApplayService;
import com.keke.utils.SystemException;

@WebServlet("/api/MakeApplay")
public class MakeApplay extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static ApplayService service = new ApplayService();

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		return;
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String data = (String) req.getAttribute("data");
		PrintWriter out = resp.getWriter();
		String info = null;
		String type = "success";
		try {
			info = service.makeApplay(data);
			out.print(" {\"isOk\":true,"
		       		+ "\"info\":\""+info+"\","
		       		+ "\"type\":\""+type+"\"}");
		} catch (SystemException e) {
			info = e.toString().split(": ")[1];
			type="fail";
			out.print(" {\"isOk\":false,"
		       		+ "\"info\":\""+info+"\","
		       		+ "\"type\":\""+type+"\"}");
		}
		
	} 
	 
}